package com.fyx.timedTask;


import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fyx.config.FiveMongoConfig;
import com.fyx.config.RequestConfig;
import com.fyx.config.SecondaryMongoConfig;
import com.fyx.config.SixthMongoConfig;
import com.fyx.entity.*;
import com.fyx.mapper.*;
import com.fyx.modules.system.repository.UserRepository;
import com.fyx.until.DateUtil;
import com.fyx.until.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import org.springframework.util.unit.DataUnit;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 广告记录
 */
@Slf4j
@Component(value="AdvertisingTask")
public class AdvertisingTask {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Resource(name = SecondaryMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate secondaryMongoTemplate;

    @Resource(name = FiveMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate fiveMongoTemplate;

    @Resource(name = SixthMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate sixthMongoTemplate;

  //  @Resource(name = NinthMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate ninthMongoTemplate;

 //   @Resource(name = TenthMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate tenthhMongoTemplate;

    @Autowired
    private BasisDataMapper basisDataMapper;


    @Autowired
    private DiamondCountMapper diamondCountMapper;

    @Autowired
    private SysDictDetailMapper sysDictDetailMapper;

    @Autowired
    private GoldRecCountMapper goldRecCountMapper;



    public void run(){
        long time = DateUtil.getDayBegin().getTime()/1000;

        Long endTime = DateUtil.getDayEnd().getTime() / 1000;
        long nowTime=new Date().getTime()/1000;
       // recordGold(time,endTime,nowTime);
        if(time==nowTime){

            time=DateUtil.getBeginDayOfYesterday().getTime()/1000;
        }
        record(time);

        log.info("广告记录任务：执行完毕=======================");
    }
    public void recordGold(Long time,Long endTime,Long nowTime) {

        String tableName, tallTable;
        if (DateUtil.getNowMonth() > 9) {
            tableName = "gold_rec_" + DateUtil.getNowYear() + DateUtil.getNowMonth();
            tallTable = "tally_rec_" + DateUtil.getNowYear() + DateUtil.getNowMonth();
        } else {
            tableName = "gold_rec_" + DateUtil.getNowYear() + "0" + DateUtil.getNowMonth();
            tallTable = "tally_rec_" + DateUtil.getNowYear() + "0" + DateUtil.getNowMonth();
        }
        if (time == nowTime) {
            time = DateUtil.getBeginDayOfYesterday().getTime() / 1000;
            if (!DateUtil.timeStamp2Date(time + "", "yyyyMM").equals(DateUtil.timeStamp2Date(nowTime + "", "yyyyMM"))) {
                tableName = "gold_rec_" + DateUtil.timeStamp2Date(time + "", "yyyyMM");
                tallTable = "tally_rec_" + DateUtil.timeStamp2Date(time + "", "yyyyMM");
            }
        }
        String str = "";
        QueryWrapper<SysDictDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_id", 16);
        wrapper.le("dict_sort", 931);
        wrapper.orderByDesc("dict_sort");
        List<SysDictDetail> list = sysDictDetailMapper.selectList(wrapper);
        SysDictDetail sysDictDetail1 = new SysDictDetail();
        sysDictDetail1.setValue("total");
        sysDictDetail1.setDictSort(999);
        list.add(sysDictDetail1);
        Aggregation aggregation;
        AggregationResults<StaticeEntity> ar;
        List<StaticeEntity> list1;
        if (list.size() > 0) {
            for (SysDictDetail sysDictDetail : list) {
                str = "";
                long v4 = 0, v5 = 0;
                Criteria criteria = new Criteria();
                criteria.and("time").gte(time).lt(endTime);
                criteria.and("way").ne(54);
                criteria.and("num").gt(0);
                if (!sysDictDetail.getValue().equals("total")) {
                    if (sysDictDetail.getDictSort() == 900) {
                        criteria.and("channel").regex(sysDictDetail.getValue().split("_")[0]);
                    } else {
                        criteria.and("channel").is(sysDictDetail.getValue());
                    }
                }
                aggregation = Aggregation.newAggregation(
                        Aggregation.match(criteria),
                        Aggregation.group().sum("num").as("v5")
                );
                ar = secondaryMongoTemplate.aggregate(aggregation, tableName, StaticeEntity.class);
                list1 = ar.getMappedResults();
                if (list1.size() > 0) {
                    str = str + "@" + list1.get(0).getV5();
                    v4 = list1.get(0).getV5();
                } else {
                    str = str + "@0";
                }
                if (sysDictDetail.getValue().equals("total")) {
                    criteria = new Criteria();
                    criteria.and("time").gte(time).lt(endTime);
                    criteria.and("is_robot").is(true);
                    aggregation = Aggregation.newAggregation(
                            Aggregation.match(criteria),
                            Aggregation.group().sum("num").as("v5")
                    );
                    ar = secondaryMongoTemplate.aggregate(aggregation, tallTable, StaticeEntity.class);
                    list1 = ar.getMappedResults();
                    if (list1.size() > 0) {
                        if (list1.get(0).getV5() < 0) {
                            v4 = v4 - list1.get(0).getV5();
                            str = "@" + v4;
                        } else if (list1.get(0).getV5() > 0) {
                            v5 = list1.get(0).getV5();
                        }
                    }
                }


                criteria = new Criteria();
                criteria.and("time").gte(time).lt(endTime);
                criteria.and("way").ne(54);
                criteria.and("num").lt(0);
                if (!sysDictDetail.getValue().equals("total")) {
                    if (sysDictDetail.getDictSort() == 900) {
                        criteria.and("channel").regex(sysDictDetail.getValue().split("_")[0]);
                    } else {
                        criteria.and("channel").is(sysDictDetail.getValue());
                    }
                }
                aggregation = Aggregation.newAggregation(
                        Aggregation.match(criteria),
                        Aggregation.group().sum("num").as("v5")
                );
                ar = secondaryMongoTemplate.aggregate(aggregation, tableName, StaticeEntity.class);
                list1 = ar.getMappedResults();
                if (list1.size() > 0) {
                    str = str + "@" + (list1.get(0).getV5() - v5);
                } else {
                    str = str + "@0";
                }

                criteria = new Criteria();
                if (!sysDictDetail.getValue().equals("total")) {
                    if (sysDictDetail.getDictSort() == 900) {
                        criteria.and("channel").regex(sysDictDetail.getValue().split("_")[0]);
                    } else {
                        criteria.and("channel").is(sysDictDetail.getValue());
                    }
                }
                aggregation = Aggregation.newAggregation(
                        Aggregation.match(criteria),
                        Aggregation.group().sum("gold").as("v5")
                );
                ar = mongoTemplate.aggregate(aggregation, "user", StaticeEntity.class);
                list1 = ar.getMappedResults();
                if (list1.size() > 0) {
                    str = str + "@" + list1.get(0).getV5();
                } else {
                    str = str + "@0";
                }
                GoldRecCount goldRecCount = new GoldRecCount();
                goldRecCount.setChannel(sysDictDetail.getValue());
                goldRecCount.setTime(time.intValue());
                goldRecCount.setGoldCount(str);
                if (!sysDictDetail.getValue().equals("total") && sysDictDetail.getDictSort() != 900) {
                    goldRecCount.setTheFather(sysDictDetail.getValue().split("_")[0].replace("h5", "") + "_total");
                } else if (sysDictDetail.getDictSort() == 900) {
                    goldRecCount.setTheFather("total");
                }
                QueryWrapper wrapper1 = new QueryWrapper<>();
                wrapper1.eq("channel", goldRecCount.getChannel());
                wrapper1.eq("time", time);
                if(goldRecCount.getTheFather()!=null) {
                    wrapper1.eq("the_father", goldRecCount.getTheFather());
                }
                GoldRecCount goldRecCount1 = goldRecCountMapper.selectOne(wrapper1);
                if (goldRecCount1 == null) {
                    goldRecCountMapper.insert(goldRecCount);
                } else {
                    goldRecCountMapper.update(goldRecCount, wrapper1);
                }

            }
        }
    }
    public void record(Long time){
        //钻石统计
        QueryWrapper<SysDictDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_id", 16);
        wrapper.le("dict_sort", 930);
        wrapper.orderByDesc("dict_sort");
        List<SysDictDetail> sysDictDetailList = sysDictDetailMapper.selectList(wrapper);
        SysDictDetail sysDictDetail1 = new SysDictDetail();
        sysDictDetail1.setValue("total");
        sysDictDetail1.setDictSort(999);
        sysDictDetailList.add(sysDictDetail1);
        sysDictDetail1 = new SysDictDetail();
        sysDictDetail1.setValue("totalcl");
        sysDictDetail1.setDictSort(998);
        sysDictDetailList.add(sysDictDetail1);
        sysDictDetail1 = new SysDictDetail();
        sysDictDetail1.setValue("totalapp");
        sysDictDetail1.setDictSort(997);
        sysDictDetailList.add(sysDictDetail1);
        for(SysDictDetail sysDictDetail:sysDictDetailList) {
            System.out.println(sysDictDetail);
            DiamondCount diamondCount = new DiamondCount();
            diamondCount.setTime(time);
            //总钻石量
            Criteria  criteria = new Criteria();
            if (sysDictDetail.getDictSort()<996) {
                if (sysDictDetail.getDictSort() == 900||sysDictDetail.getDictSort() == 910||sysDictDetail.getDictSort() == 920) {
                    criteria.and("channel").regex(sysDictDetail.getValue().split("_")[0]);
                } else {
                    criteria.and("channel").is(sysDictDetail.getValue());
                }
            }
            Aggregation aggregation = Aggregation.newAggregation(
                    Aggregation.match(criteria),
                    Aggregation.group().sum("diamond").as("diamond")
            );
            AggregationResults<UserEntity> ar;
            if(sysDictDetail.getValue().indexOf("h5")>=0||sysDictDetail.getDictSort()==910||sysDictDetail.getDictSort()==900||sysDictDetail.getDictSort() > 997){
                if(sysDictDetail.getValue().indexOf("cl_")>0||sysDictDetail.getValue().equals("totalcl")){
                    ar = fiveMongoTemplate.aggregate(aggregation, "user", UserEntity.class);
                }
                else {
                    ar = mongoTemplate.aggregate(aggregation, "user", UserEntity.class);
                }
            }else {
                ar =ninthMongoTemplate.aggregate(aggregation, "user", UserEntity.class);
            }
//            //ar = mongoTemplate.aggregate(aggregation, "user", UserEntity.class);
            List<UserEntity> list1 = ar.getMappedResults();
            if (list1.size() > 0) {
             diamondCount.setAllDiamond(list1.get(0).getDiamond());
             }
            String collectionName = "diamond_rec_";
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM");
            String dateStr = ft.format(DateUtil.getDayBegin());
            dateStr = dateStr.replace("-", "").trim();
            collectionName = collectionName + dateStr;

            //钻石总购买
            criteria = new Criteria();
            if (sysDictDetail.getDictSort()<996) {
                if (sysDictDetail.getDictSort() == 900||sysDictDetail.getDictSort() == 910||sysDictDetail.getDictSort() == 920) {
                    criteria.and("channel").regex(sysDictDetail.getValue().split("_")[0]);
                } else {
                    criteria.and("channel").is(sysDictDetail.getValue());
                }
            }
            criteria.and("time").gte(time).lte(time+86400);
            criteria.and("num").gt(0);
            Aggregation aggregation1 = Aggregation.newAggregation(
                    Aggregation.match(criteria),
                    Aggregation.group().sum("num").as("num")
            );
            AggregationResults<DiamondRecEntity> ar1;
            if(sysDictDetail.getValue().indexOf("h5")>=0||sysDictDetail.getDictSort()==910||sysDictDetail.getDictSort()==900||sysDictDetail.getDictSort() > 997){
                if(sysDictDetail.getValue().indexOf("cl_")>0||sysDictDetail.getValue().equals("totalcl")){
                    ar1  = sixthMongoTemplate.aggregate(aggregation1, collectionName, DiamondRecEntity.class);
                }
                else {
                    ar1   = secondaryMongoTemplate.aggregate(aggregation1, collectionName, DiamondRecEntity.class);
                }
            }else {
                ar1   = tenthhMongoTemplate.aggregate(aggregation1, collectionName, DiamondRecEntity.class);
            }
            List<DiamondRecEntity> list2 = ar1.getMappedResults();
            if (list2.size() > 0) {
                diamondCount.setBuyDiamond(list2.get(0).getNum());
            }

            //钻石总赠送暂时先放着

            //钻石总回收
            criteria = new Criteria();
            if (sysDictDetail.getDictSort()<996) {
                if (sysDictDetail.getDictSort() == 900||sysDictDetail.getDictSort() == 910||sysDictDetail.getDictSort() == 920) {
                    criteria.and("channel").regex(sysDictDetail.getValue().split("_")[0]);
                } else {
                    criteria.and("channel").is(sysDictDetail.getValue());
                }
            }
            criteria.and("time").gte(time).lte(time+86400);
            criteria.and("num").lt(0);
            aggregation1 = Aggregation.newAggregation(
                    Aggregation.match(criteria),
                    Aggregation.group().sum("num").as("num")
            );
            if(sysDictDetail.getValue().indexOf("h5")>=0||sysDictDetail.getDictSort()==910||sysDictDetail.getDictSort()==900||sysDictDetail.getDictSort() > 997){
                if(sysDictDetail.getValue().indexOf("cl_")>0||sysDictDetail.getValue().equals("totalcl")){
                    ar1  =sixthMongoTemplate.aggregate(aggregation1, collectionName, DiamondRecEntity.class);
                }
                else {
                    ar1   = secondaryMongoTemplate .aggregate(aggregation1, collectionName, DiamondRecEntity.class);
                }
            }else {
                ar1   = tenthhMongoTemplate.aggregate(aggregation1, collectionName, DiamondRecEntity.class);
            }
            list2 = ar1.getMappedResults();
            if (list2.size() > 0) {
                diamondCount.setRecycleDiamond(list2.get(0).getNum());
            }

            //活跃人数总钻石
            criteria = new Criteria();
            if (sysDictDetail.getDictSort()<996) {
                if (sysDictDetail.getDictSort() == 900||sysDictDetail.getDictSort() == 910||sysDictDetail.getDictSort() == 920) {
                    criteria.and("channel").regex(sysDictDetail.getValue().split("_")[0]);
                } else {
                    criteria.and("channel").is(sysDictDetail.getValue());
                }
            }
            criteria.and("last_time").gte(time).lte(time+86400);
            aggregation = Aggregation.newAggregation(
                    Aggregation.match(criteria),
                    Aggregation.group().sum("diamond").as("diamond")
            );
            if(sysDictDetail.getValue().indexOf("h5")>=0||sysDictDetail.getDictSort()==910||sysDictDetail.getDictSort()==900||sysDictDetail.getDictSort() > 997){
                if(sysDictDetail.getValue().indexOf("cl_")>0||sysDictDetail.getValue().equals("totalcl")){
                    ar = fiveMongoTemplate.aggregate(aggregation, "user", UserEntity.class);
                }
                else {
                    ar = mongoTemplate.aggregate(aggregation, "user", UserEntity.class);
                }
            }else {
                ar =ninthMongoTemplate.aggregate(aggregation, "user", UserEntity.class);
            }
            list1 = ar.getMappedResults();
            if (list1.size() > 0) {
                diamondCount.setActiveDiamond(list1.get(0).getDiamond());
            }
            diamondCount.setChannel(sysDictDetail.getValue());
            diamondCount.setTime(time);
            if (sysDictDetail.getDictSort() < 900) {
                if(sysDictDetail.getDictSort()>40&&sysDictDetail.getValue().indexOf("h5")==-1){
                    diamondCount.setTheFather(sysDictDetail.getValue().split("_")[0]+"_app_total");
                }else {
                 diamondCount.setTheFather(sysDictDetail.getValue().split("_")[0].replace("h5", "") + "_total");
                }
            } else if (sysDictDetail.getDictSort() == 900) {
                diamondCount.setTheFather("total");
            }else if (sysDictDetail.getDictSort() == 910) {
                diamondCount.setTheFather("totalcl");
            }else if (sysDictDetail.getDictSort() == 920) {
                diamondCount.setTheFather("totalapp");
            }

            QueryWrapper<DiamondCount> wrapper3 = new QueryWrapper<>();
            wrapper3.eq("channel", diamondCount.getChannel());
            wrapper3.eq("time", time-24*3600);
            if(diamondCount.getTheFather()!=null) {
                wrapper3.eq("the_father", diamondCount.getTheFather());
            }
            DiamondCount diamondCount2 = diamondCountMapper.selectOne(wrapper3);
            if(diamondCount2==null){
                //diamondCount.setAllDiamond(  (diamondCount.getBuyDiamond()==null?0:diamondCount.getBuyDiamond()) + (diamondCount.getRecycleDiamond()==null?0:diamondCount.getRecycleDiamond()));
            }else{
               // diamondCount.setAllDiamond(diamondCount2.getAllDiamond() + (diamondCount.getBuyDiamond()==null?0:diamondCount.getBuyDiamond()) + (diamondCount.getRecycleDiamond()==null?0:diamondCount.getRecycleDiamond()));
            }
            QueryWrapper<DiamondCount> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("channel", diamondCount.getChannel());
            wrapper2.eq("time", time);
            if(diamondCount.getTheFather()!=null) {
                wrapper2.eq("the_father", diamondCount.getTheFather());
            }
            DiamondCount diamondCount1 = diamondCountMapper.selectOne(wrapper2);
            if (diamondCount1 == null) {
                diamondCountMapper.insert(diamondCount);
            } else {
                diamondCountMapper.update(diamondCount, wrapper2);
            }
//            QueryWrapper<DiamondCount> wrapper2 = new QueryWrapper<>();
//            wrapper2.eq("time", time);
//            diamondCountMapper.delete(wrapper2);
//            diamondCountMapper.insert(diamondCount);
        }

    }
//    public void record() {
//
//        long time = DateUtil.getDayBegin().getTime() / 1000;
//        long endTime = DateUtil.getDayEnd().getTime() / 1000;
//        long nowTime=new Date().getTime()/1000;
//        String tableName;
//        if (DateUtil.getNowMonth() > 9) {
//            tableName = "collect_data_" + DateUtil.getNowYear() + DateUtil.getNowMonth();
//        } else {
//            tableName = "collect_data_" + DateUtil.getNowYear() + "0" + DateUtil.getNowMonth();
//        }
//        if(time==nowTime){
//            time=DateUtil.getBeginDayOfYesterday().getTime()/1000;
//            if(!DateUtil.timeStamp2Date(time+"","yyyyMM").equals(DateUtil.timeStamp2Date(nowTime+"","yyyyMM"))){
//                tableName = "collect_data_"+DateUtil.timeStamp2Date(time+"","yyyyMM");
//            }
//        }
//
//        //广告数据
//        AdvertisingEntity advertisingEntity = new AdvertisingEntity();
//        advertisingEntity.setTime(time);
//
//        //基础数据
//        BasisData basisData = new BasisData();
//        basisData.setTime(time);
//
//        //统计当日看广告的次数
//        Criteria criteria = new Criteria();
//        criteria.and("time").gte(time).lte(endTime);
//        criteria.and("evid").gte(1000000044).lte(1000000059);
//
//
//        criteria.and("value").is(1);
//        long totale = secondaryMongoTemplate.count(new Query().addCriteria(criteria), tableName);
//        advertisingEntity.setClick_num((int) totale);
//        basisData.setTodayClick(totale);
//
//        criteria = new Criteria();
//        criteria.and("time").gte(time).lte(endTime);
//        criteria.and("evid").gte(1000000044).lte(1000000059);
//
//
//        totale = secondaryMongoTemplate.count(new Query().addCriteria(criteria), tableName);
//        advertisingEntity.setAll_click_num((int) totale);
//        criteria.and("value").is(1);
//        //统计当日看广告的人数
//        Aggregation aggregation = Aggregation.newAggregation(
//                Aggregation.match(criteria),
//                Aggregation.group("numid").count().as("num")
//        );
//        AggregationResults<TaskEntity> ar = secondaryMongoTemplate.aggregate(aggregation, tableName, TaskEntity.class);
//        List<TaskEntity> list = ar.getMappedResults();
//        if (list.size() > 0) {
//            advertisingEntity.setLook_over_num(list.size());
//        }
//        basisData.setWatchAdac((long)advertisingEntity.getLook_over_num());
//        //充值过当日看广告的人数
//        aggregation = Aggregation.newAggregation(
//                Aggregation.match(criteria),
//                Aggregation.group("numid").count().as("id")
//        );
////        AggregationResults<PayUserEntity> ar1=secondaryMongoTemplate.aggregate(aggregation, tableName, PayUserEntity.class);
////        List<PayUserEntity> listUser = ar1.getMappedResults();
////        String userArry[]=new String[listUser.size()];
////        int i=0;
////        if(listUser.size()>0){
////            for(PayUserEntity userEntity:listUser){
////                userArry[i]=userEntity.getId();
////                i++;
////            }
////        }
////        criteria = new Criteria();
////        criteria.and("id").in(userArry);
////        criteria.and("all_fee").gt(0);
////        criteria.and("last_time").gte(time);
////        aggregation = Aggregation.newAggregation(
////                Aggregation.match(criteria),
////                Aggregation.group("id").count().as("num"),
////                Aggregation.group("id").count().as("num")
////        );
////        ar = mongoTemplate.aggregate(aggregation, "user", TaskEntity.class);
////        list = ar.getMappedResults();
////        if (list.size() > 0) {
////            advertisingEntity.setTodayClick(list.get(0).getNum());
////        }
//        //当日注册且活跃用户大于1-8的总人数
//        criteria = new Criteria();
//        criteria.and("time").gte(time).lte(endTime);
//        criteria.and("evid").gte(1000000044).lte(1000000059);
//
//
//        criteria.and("value").is(1);
//        criteria.and("regtime").gte(time);
//        aggregation = Aggregation.newAggregation(
//                Aggregation.match(criteria),
//                Aggregation.group("numid").count().as("num"),
//                Aggregation.match(Criteria.where("num").lte(8)),
//                Aggregation.group("numid").count().as("num")
//        );
//        ar = secondaryMongoTemplate.aggregate(aggregation, tableName, TaskEntity.class);
//        list = ar.getMappedResults();
//        if (list.size() > 0) {
//            advertisingEntity.setRegNum1(list.get(0).getNum());
//        }
//        //当日注册且活跃用户大于1-8的总次数
//        criteria = new Criteria();
//        criteria.and("time").gte(time).lte(endTime);
//        criteria.and("evid").gte(1000000044).lte(1000000059);
//
//
//        criteria.and("value").is(1);
//        criteria.and("regtime").gte(time);
//        aggregation = Aggregation.newAggregation(
//                Aggregation.match(criteria),
//                Aggregation.group("numid").count().as("num"),
//                Aggregation.match(Criteria.where("num").lte(8)),
//                Aggregation.group("numid").sum("num").as("num")
//
//        );
//        ar = secondaryMongoTemplate.aggregate(aggregation, tableName, TaskEntity.class);
//        list = ar.getMappedResults();
//        if (list.size() > 0) {
//            advertisingEntity.setRegNumCount1(list.get(0).getNum());
//        }
//
//        //当日注册且活跃用户大于8的总人数
//        criteria = new Criteria();
//        criteria.and("time").gte(time).lte(endTime);
//        criteria.and("evid").gte(1000000044).lte(1000000059);
//
//
//        criteria.and("value").is(1);
//        criteria.and("regtime").gte(time);
//        aggregation = Aggregation.newAggregation(
//                Aggregation.match(criteria),
//                Aggregation.group("numid").count().as("num"),
//                Aggregation.match(Criteria.where("num").gt(8)),
//                Aggregation.group("numid").count().as("num")
//
//        );
//        ar = secondaryMongoTemplate.aggregate(aggregation, tableName, TaskEntity.class);
//        list = ar.getMappedResults();
//        if (list.size() > 0) {
//            advertisingEntity.setRegNum2(list.get(0).getNum());
//        }
//        //当日注册且活跃用户大于8的总次数
//        criteria = new Criteria();
//        criteria.and("time").gte(time).lte(endTime);
//        criteria.and("evid").gte(1000000044).lte(1000000059);
//
//
//        criteria.and("value").is(1);
//        criteria.and("regtime").gte(time);
//        aggregation = Aggregation.newAggregation(
//                Aggregation.match(criteria),
//                Aggregation.group("numid").count().as("num"),
//                Aggregation.match(Criteria.where("num").gt(8)),
//                Aggregation.group("numid").sum("num").as("num")
//
//        );
//        ar = secondaryMongoTemplate.aggregate(aggregation, tableName, TaskEntity.class);
//        list = ar.getMappedResults();
//        if (list.size() > 0) {
//            advertisingEntity.setRegNumCount2(list.get(0).getNum());
//        }
//
//        //非当日注册且活跃用户大于1-8的总人数
//        criteria = new Criteria();
//        criteria.and("time").gte(time).lte(endTime);
//        criteria.and("evid").gte(1000000044).lte(1000000059);
//
//
//        criteria.and("value").is(1);
//        criteria.and("regtime").lt(time);
//        aggregation = Aggregation.newAggregation(
//                Aggregation.match(criteria),
//                Aggregation.group("numid").count().as("num"),
//                Aggregation.match(Criteria.where("num").lte(8)),
//                Aggregation.group("numid").count().as("num")
//
//        );
//        ar = secondaryMongoTemplate.aggregate(aggregation, tableName, TaskEntity.class);
//        list = ar.getMappedResults();
//        if (list.size() > 0) {
//            advertisingEntity.setNotRegNum1(list.get(0).getNum());
//        }
//        //非当日注册且活跃用户大于1-8的总次数
//        criteria = new Criteria();
//        criteria.and("time").gte(time).lte(endTime);
//        criteria.and("evid").gte(1000000044).lte(1000000059);
//
//
//        criteria.and("value").is(1);
//        criteria.and("regtime").lt(time);
//        aggregation = Aggregation.newAggregation(
//                Aggregation.match(criteria),
//                Aggregation.group("numid").count().as("num"),
//                Aggregation.match(Criteria.where("num").lte(8)),
//                Aggregation.group("numid").sum("num").as("num")
//
//        );
//        ar = secondaryMongoTemplate.aggregate(aggregation, tableName, TaskEntity.class);
//        list = ar.getMappedResults();
//        if (list.size() > 0) {
//            advertisingEntity.setNotRegNumCount1(list.get(0).getNum());
//        }
//
//        //非当日注册且活跃用户大于8的总人数
//        criteria = new Criteria();
//        criteria.and("time").gte(time).lte(endTime);
//        criteria.and("evid").gte(1000000044).lte(1000000059);
//
//
//        criteria.and("value").is(1);
//        criteria.and("regtime").lt(time);
//        aggregation = Aggregation.newAggregation(
//                Aggregation.match(criteria),
//                Aggregation.group("numid").count().as("num"),
//                Aggregation.match(Criteria.where("num").gt(8)),
//                Aggregation.group("numid").count().as("num")
//
//        );
//        ar = secondaryMongoTemplate.aggregate(aggregation, tableName, TaskEntity.class);
//        list = ar.getMappedResults();
//        if (list.size() > 0) {
//            advertisingEntity.setNotRegNum2(list.get(0).getNum());
//        }
//        //非当日注册且活跃用户大于8的总次数
//        criteria = new Criteria();
//        criteria.and("time").gte(time).lte(endTime);
//        criteria.and("evid").gte(1000000044).lte(1000000059);
//
//
//        criteria.and("value").is(1);
//        criteria.and("regtime").lt(time);
//        aggregation = Aggregation.newAggregation(
//                Aggregation.match(criteria),
//                Aggregation.group("numid").count().as("num"),
//                Aggregation.match(Criteria.where("num").gt(8)),
//                Aggregation.group("numid").sum("num").as("num")
//
//        );
//        ar = secondaryMongoTemplate.aggregate(aggregation, tableName, TaskEntity.class);
//        list = ar.getMappedResults();
//        if (list.size() > 0) {
//            advertisingEntity.setNotRegNumCount2(list.get(0).getNum());
//        }
//        mongoTemplate.remove(new Query(Criteria.where("time").is(time)), AdvertisingEntity.class);
//        mongoTemplate.insert(advertisingEntity);
//
//       basisData.setNewWatchAdac(advertisingEntity.getRegNum1()+advertisingEntity.getRegNum2());
//       basisData.setNewTodayClick(advertisingEntity.getRegNumCount1()+advertisingEntity.getRegNumCount2());
//       basisData.setOldWatchAdac(advertisingEntity.getNotRegNum1()+advertisingEntity.getNotRegNum2());
//       basisData.setOldTodayClick(advertisingEntity.getNotRegNumCount1()+advertisingEntity.getNotRegNumCount2());
//        criteria = new Criteria();
//        criteria.and("reg_time").gte(time);
//        totale= mongoTemplate.count(new Query().addCriteria(criteria), "user");
//        basisData.setNewUser(totale);
//        criteria = new Criteria();
//        criteria.and("reg_time").lt(time);
//        criteria.and("last_time").gte(time).lte(endTime);
//        totale= mongoTemplate.count(new Query().addCriteria(criteria), "user");
//        basisData.setOldUser(totale);
//        basisData.setActNum((int)(basisData.getNewUser()+basisData.getOldUser()));
//        QueryWrapper<BasisData> wrapper=new QueryWrapper<>();
//        wrapper.eq("time",basisData.getTime());
//        basisDataMapper.delete(wrapper);
//        basisDataMapper.insert(basisData);
//    }
//
}
